package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.*;

@Controller
@RequestMapping("/guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cmfzGuruService;

    @RequestMapping("/selectCmfzGuruDatagrid")
    @ResponseBody
    public Map selectCmfzGuruDatagrid(int page,int rows){
        return cmfzGuruService.selectCmfzGuruDatagrid(page,rows);
    }

    @RequestMapping("/deleteSomeGurus")
    @ResponseBody
    public Map deleteSomeGurus(int[] ids){
        Map map=new HashMap();
        try {
            map.put("isDelete",true);
            cmfzGuruService.deleteSomeGurus(ids);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete",false);
        }
        return map;
    }

    @RequestMapping("/addGuru")
    @ResponseBody
    public Map addGuru(MultipartFile mf, CmfzGuru cmfzGuru, HttpSession session){
        Map map=new HashMap();
        //获取文件路径
        String path=session.getServletContext().getRealPath("/img/");
        //若目录不存在，创建该目录
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        //获取老名字
        String oldName=mf.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        //相对路径
        String realPath="/img/"+newName;
        //绝对路径
        String absolPath=session.getServletContext().getRealPath(realPath);

        try {
            mf.transferTo(new File(absolPath));
            //把上师对象添加到数据库表里面
            cmfzGuru.setGuruImage(realPath);
            cmfzGuruService.insertGuru(cmfzGuru);
            map.put("isAdd",true);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }


    @RequestMapping("/exportGuruXLS")
    @ResponseBody
    public Map exportGuruXLS() throws Exception {
        return cmfzGuruService.exportGuruXLS();
    }

    @RequestMapping("/importGuruXLS")
    @ResponseBody
    public Map importGuruXLS() throws Exception {
       return cmfzGuruService.importGuruXLS();
    }



   /* @RequestMapping("/exportGuruXLS")
    @ResponseBody
    public Map exportGuruXLS() throws Exception {
        Map map=new HashMap();
        //    准备数据
        List<String> titles = Arrays.asList("编号","姓名","图片","法号","状态");
//    数据查询 上师全部的数据
        List<CmfzGuru> cmfzGuruList = cmfzGuruService.selectAllGurus();
//    1. 新建一个HSSFWorkbook 对象
        HSSFWorkbook workbook=new HSSFWorkbook();
//    单元格样式对象
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setColor((short)10);
        font.setFontName("宋体");
        cellStyle.setFont(font);
//    2. 新建一个HSSFSheet对象
        HSSFSheet sheet = workbook.createSheet("cmfzGuru");
//    3. 写入标题栏数据
//      创建行
        HSSFRow row = sheet.createRow(0);

        for (int i = 0; i < titles.size(); i++) {
            //      创建单元格
            HSSFCell cell = row.createCell(i);
            //      添加样式
            cell.setCellStyle(cellStyle);

            //      写入数据
            cell.setCellValue(titles.get(i));
        }

//    4. 写入上师数据
        for (int i = 0; i < cmfzGuruList.size(); i++) {
            CmfzGuru cmfzGuru=cmfzGuruList.get(i);
            //      创建行
            HSSFRow row1 = sheet.createRow(i+1);
//      创建单元格
            //编号
            HSSFCell cell = row1.createCell(0);
            cell.setCellValue(cmfzGuru.getGuruId());

            //姓名
            HSSFCell cell1 = row1.createCell(1);
            cell1.setCellValue(cmfzGuru.getGuruName());

            //图片
            HSSFCell cell2 = row1.createCell(2);
            cell2.setCellValue(cmfzGuru.getGuruImage());

            //法号
            HSSFCell cell3 = row1.createCell(3);
            cell3.setCellValue(cmfzGuru.getGuruNickname());

            //状态
            HSSFCell cell4 = row1.createCell(4);
            cell4.setCellValue(cmfzGuru.getGuruStatus());

        }

//    5. 输出
        workbook.write(new FileOutputStream(new File("D://cmfzGuru.xls")));
        map.put("isExport","D://cmfzGuru.xls");
        return map;
    }
*/

  /*  @RequestMapping("/importGuruXLS")
    @ResponseBody
    public Map importGuruXLS() throws Exception {
        Map map=new HashMap();

        ArrayList<CmfzGuru> cmfzGuruList = new ArrayList<>();
//    1.读取文件
        FileInputStream fileInputStream=new FileInputStream("D://cmfzGuru.xls");

//    2.解析流得到工作簿对象
        HSSFWorkbook workbook=new HSSFWorkbook(fileInputStream);

//    3.获取表
        HSSFSheet sheet = workbook.getSheet("cmfzGuru");

//    4.获取行 1----最后一行
        int lastRowNum = sheet.getLastRowNum();

//    5.获取数据封装在集合中
        for (int i = 1; i < lastRowNum; i++) {
//      获取行
            CmfzGuru cmfzGuru=new CmfzGuru();
            HSSFRow row = sheet.getRow(i);

//      获取单元格和数据
            //编号
            HSSFCell cell = row.getCell(0);
            double numericCellValue = cell.getNumericCellValue();
            int guruId=(int)numericCellValue;
            cmfzGuru.setGuruId(guruId);

            //姓名
            HSSFCell cell1 = row.getCell(1);
            String guruName = cell1.getStringCellValue();
            cmfzGuru.setGuruName(guruName);

            //图片
            HSSFCell cell2 = row.getCell(2);
            String guruImage = cell2.getStringCellValue();
            cmfzGuru.setGuruImage(guruImage);

            //法号
            HSSFCell cell3 = row.getCell(3);
            String guruNickname = cell3.getStringCellValue();
            cmfzGuru.setGuruNickname(guruNickname);

            //状态
            HSSFCell cell4 = row.getCell(4);
            double numericCellValue1 = cell4.getNumericCellValue();
            int guruStatus=(int)numericCellValue1;
            cmfzGuru.setGuruStatus(guruStatus);

//      封装好的对象放入集合
            cmfzGuruList.add(cmfzGuru);
        }

//    6.添加数据库
        for (CmfzGuru cmfzGuru : cmfzGuruList) {
            try {
                cmfzGuruService.insertGuru(cmfzGuru);
                map.put("isImport",true);
            } catch (Exception e) {
                e.printStackTrace();
                map.put("isImport",false);
            }
        }
        return map;
    }*/
}
